const logger = require("../logger.js")

const Message = require("../message.js")
/**
 * 统计连接信息
 */
class StatisticsController{
    constructor(roomMap) {
        this._roomMap = roomMap
    }

    stats(){
        this._statsIntervalId = setInterval(async () => {
            this._roomMap.getRooms().forEach((room, id) => {
                room.getMembers().forEach((member, memberId)=> {
                    member.getTransport().forEach(async (transport, transportId)=> {
                      const stats = await transport.getTransportStats()
                      stats.forEach((item, index) => {
                          if (item.rtpSendBitrate !== undefined && item.availableOutgoingBitrate !== undefined && item.rtpPacketLossSent !== undefined) {
                              if (item.rtpSendBitrate > item.availableOutgoingBitrate) {
                                  member.client.send(Message.encoder(Message.msgType.statistics, item))
                              }
                          }
                      })
                    })
                })
            })
        }, 5000)
    }

    release(){
        if (this._statsIntervalId !== undefined) {
            clearInterval(this._statsIntervalId)
        }
    }
}

module.exports = {StatisticsController}
